﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using IntroMVC4.Areas.AnnaKids.Models;
using IntroMVC4.Models.Interface;
using IntroMVC4.Models.Services;

namespace IntroMVC4.Areas.AnnaKids.Controllers
{
    public class AdminController :Controller//: BaseAdminController
    {
        IProduct _iProduct;
        public AdminController()
        {
            _iProduct = new ProductService();
        }
        public AdminController(IProduct iProduct)
        {
            _iProduct = iProduct;
        }
        //
        // GET: /AnnaKids/Admin/

        public ActionResult QuanTri()
        {
            return View();
        }
        public ActionResult SanPham()
        {
            return View();
        }
        #region Phieu Nhap
        public ActionResult QuanLyPhieuNhap()
        {
            return View();
        }
        public ActionResult ThemPhieuNhap()
        {
            List<SelectListItem> storeTypes = (from t in _iProduct.GetPrincipalCategories()
                                               select new SelectListItem
                                               {
                                                   Text = t.Name,
                                                   Value = t.Id.ToString()
                                               }
                          ).ToList();
            ViewData["Products"] = storeTypes;
            return View(new IntroMVC4.Models.Domain.InputFromEdit());
        }
        [HttpPost]
        public ActionResult ThemPhieuNhap(FormCollection collection)
        {
            return View();
        }
        public ActionResult SuaPhieuNhap()
        {
            return View();
        }
        [HttpPost]
        public ActionResult SuaPhieuNhap(FormCollection collection)
        {
            return View();
        }
        [HttpPost]
        public JsonResult SearchInputForms()
        {
            return Json(_iProduct.GetAllInputForms());
        }
        #endregion
        public ActionResult PhieuXuatHang()
        {
            return View();
        }
        [HttpPost]
        public ActionResult PhieuXuatHang(FormCollection collection)
        {
            return View();
        }

       
        [HttpPost]
        public JsonResult SearchProducts()
        {
            return Json(_iProduct.GetAllProducts());
        }
        [HttpPost]
        public JsonResult SearchFilterProducts(string productName)
        {
            return Json(_iProduct.GetFilterProducts(productName));
        }
        public ActionResult SuaSanPham(string sku)
        {
            var listResult = GetDataForEditProduct(sku);
            return View(listResult);
        }
        public ActionResult ThemSanPham()
        {
            List<SelectListItem> storeTypes = (from t in _iProduct.GetPrincipalCategories()
                                               select new SelectListItem
                                               {
                                                   Text = t.Name,
                                                   Value = t.Id.ToString()
                                               }
                          ).ToList();
            List<SelectListItem> productTypes = (from t in _iProduct.GetProductCategories()
                                                 select new SelectListItem
                                                 {
                                                     Text = t.Name,
                                                     Value = t.Id.ToString()
                                                 }
                        ).ToList();
            ViewData["PrincipalCategories"] = storeTypes;
            ViewData["ProductCategories"] = productTypes;
            return View(new IntroMVC4.Models.Domain.ProductSKU());
        }
        [HttpPost]
        public ActionResult ThemSanPham(FormCollection collection)
        {
            var sku = collection["SKU"];
            var gtin = collection["GTIN"];
            var name = collection["Name"];
            var principalCategoryId = collection["PrincipalCategoryId"];
            var isActive = collection["IsActive"] == "on";
            var productCatalogId = collection["ProductCatalogId"];
            _iProduct.AddProduct(sku, gtin, name, Convert.ToInt32(principalCategoryId), isActive, Convert.ToInt32(productCatalogId));
            var listResult = GetDataForEditProduct(sku);
            return View(listResult);
        }
        private IntroMVC4.Models.Domain.ProductSKU GetDataForEditProduct(string sku)
        {
            List<SelectListItem> storeTypes = (from t in _iProduct.GetPrincipalCategories()
                                               select new SelectListItem
                                               {
                                                   Text = t.Name,
                                                   Value = t.Id.ToString()
                                               }
                        ).ToList();
            List<SelectListItem> productTypes = (from t in _iProduct.GetProductCategories()
                                                 select new SelectListItem
                                                 {
                                                     Text = t.Name,
                                                     Value = t.Id.ToString()
                                                 }
                        ).ToList();
            ViewData["PrincipalCategories"] = storeTypes;
            ViewData["ProductCategories"] = productTypes;
            var listResult = _iProduct.GetProductBySKU(sku);
            return listResult;
        }
        //[HttpPost]
        //public JsonResult ProductAttributeData(long productId)
        //{
        //    return Json(_iProduct.GetAttributeProductByProductId(productId));
        //}
        [HttpPost]
        public ActionResult SuaSanPham(FormCollection collection)
        {
            var sku = collection["SKU"];
            var gtin = collection["GTIN"];
            var name = collection["Name"];
            var principalCategoryId = collection["PrincipalCategoryId"];
            var isActive = collection["IsActive"] == "on";
            var productCatalogId = collection["ProCatalogId"];
            var productIds = collection["productId"];
            StringBuilder attParams = new StringBuilder();
            List<IntroMVC4.Models.Attribute> listAttr = _iProduct.GetListAttributeByProductCatagory(Convert.ToInt32(productCatalogId));
            foreach (IntroMVC4.Models.Attribute param in listAttr)
            {
                attParams.Append(param.Id);
                attParams.Append("=");
                attParams.Append(collection[param.Name]);
                attParams.Append(";");
            }
            _iProduct.UpdateProduct(sku, gtin, name, Convert.ToInt32(principalCategoryId), isActive, productIds, attParams.ToString());
            var listResult = GetDataForEditProduct(sku);
            return View(listResult);
        }


    }
}